//
//  String++.swift
//  EasyEverything
//
//  Created by YangZhe on 17/3/29.
//  Copyright © 2017年 kuletech. All rights reserved.
//

extension String{
    public func split(_ s:String) -> [String]{
        if s.isEmpty{
            var spiltedString=[String]()
            for y in self{
                spiltedString.append(String(y))
            }
            return spiltedString
        }
        return components(separatedBy: s)
    }
    
    public func removeEnd(){
        let _ = removeEndWithResult()
    }
    
    public func removeEndWithResult() -> String{
        var temp = self
        if temp.count > 0 {
            temp.remove(at: temp.index(before: temp.endIndex))
        }
        return temp
    }
    
    public func transformToPinYin() -> String{
        let mutableString = NSMutableString(string: self)
        CFStringTransform(mutableString, nil, kCFStringTransformToLatin, false)
        CFStringTransform(mutableString, nil, kCFStringTransformStripDiacritics, false)
        return String(mutableString).replacingOccurrences(of: " ", with: "")
    }
    
    public func toInt() -> Int? {
        return Int(self)
    }
    
    public func toFloat() -> Float? {
        return Float(self)
    }
    
    public func toDouble() -> Double? {
        return Double(self)
    }
    
    //MARK:- 去除字符串两端的空白字符
    public func trim() -> String {
        return self.trimmingCharacters(in: CharacterSet.whitespaces)
    }
    
    //MARK:- 字符串长度
    public func length() -> Int {
        return self.count
    }
    
    public func indexOf(_ target: Character) -> Int? {
        return self.index(of: target)?.encodedOffset
    }
    
    public func subString(to: Int) -> String {
        let endIndex = String.Index.init(encodedOffset: to)
        let subStr = self[self.startIndex..<endIndex]
        return String(subStr)
    }
    
    public func subString(from: Int) -> String {
        let startIndex = String.Index.init(encodedOffset: from)
        let subStr = self[startIndex..<self.endIndex]
        return String(subStr)
    }
    
    public func subString(start: Int, end: Int) -> String {
        let startIndex = String.Index.init(encodedOffset: start)
        let endIndex = String.Index.init(encodedOffset: end)
        return String(self[startIndex..<endIndex])
    }
    
    public func subString(range: Range<String.Index>) -> String {
        return String(self[range.lowerBound..<range.upperBound])
    }
}
